package qqclient.service;

import qqcommon.Message;
import qqcommon.MessageType;

import java.io.*;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class FileClientService {

    public void sendFile(String senderId,String receiverId,String srcPath,String destPath){
        //读取文件
        FileInputStream fileInputStream = null;
        byte buf[] = new byte[(int)new File(srcPath).length()];
        try {
            //创建字节输入流，添加读取路径
            fileInputStream = new FileInputStream(srcPath);
            //将要读取的文件读取到字节数组中
            fileInputStream.read(buf);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //设置消息属性
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_FILE_MES);
        message.setSender(senderId);
        message.setReceiver(receiverId);
        message.setSrcPath(srcPath);
        message.setDestPath(destPath);
        message.setFileBytes(buf);


        //获取到发送者的线程，将请求信息发送到服务端
        try {
            ObjectOutputStream oos =
                    new ObjectOutputStream(ManageClientConnectServerThread
                            .getClientConnectServerThread(senderId)
                            .getSocket()
                            .getOutputStream());

            oos.writeObject(message);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("\n你发送文件:" + srcPath + "给 " + receiverId + " 到:" + destPath);


    }


}
